<chart>    
    <axis_ticks value_ticks='true' category_ticks='true' major_thickness='2' minor_thickness='1' minor_count='1' position='outside' />
    <axis_category alpha='0' font='arial' bold='false' skip='0' orientation='horizontal' />
    <axis_value size='12' alpha='100' color='00000' />
    <chart_rect x='50' y='70' width='800' height='350' positive_color='FFFFFF' positive_alpha='30' negative_color='FFFFFF' />               
    <chart_border color='000000' top_thickness='2' bottom_thickness='2' left_thickness='1' right_thickness='1' />
    <chart_pref line_thickness='2' point_shape='none' fill_shape='false' />
    <chart_value position='cursor' size='12' color='000000'  />
    <chart_grid_h alpha='10' color='000000' thickness='1' type='solid' />
    <chart_grid_v alpha='10' color='000000' thickness='1' type='solid' />
<?php

	$criteriu = $_GET['criteriu'];
	$perioada = $_GET['perioada'];    
	$tip = $_GET['tip'];

	$user = $_GET['user'];

    if (empty($user)) $user = -100;
	$client = $_GET['client'];
    if (empty($client)) $client = -100;
	$trans = $_GET['trans'];
    if (empty($trans)) $trans = -100;
	$incarcare = $_GET['incarcare'];
    if (empty($incarcare)) $incarcare = -100;
	$descarcare = $_GET['descarcare'];
    if (empty($descarcare)) $descarcare = -100;
    
    //monezile
    $moneda = $_GET['moneda'];
    
    $USD = $_GET['USD'];
    $EUR = $_GET['EUR'];
    $RON = $_GET['RON'];
    
	require_once('paths.inc');
	require_once(INCLUDE_PATH.'constants.inc');
	require_once('page.inc');
	require_once(DB_PATH.'db.inc');
	
	require_once('security.inc');
	require_once('utils.inc');	
	require_once('time.inc');	
	
	session_start();
	$db = &db_connect();
	$id_client = $_SESSION['user_logat']->id_client;
	
	$an_curent = date("Y");
	$luna_curenta = date("m");
	$zile_luna_curenta = cal_days_in_month(CAL_GREGORIAN, $luna_curenta, $an_curent);	
		
	$data_start = date("Y-m-d");

	global $valori;
	global $cine;
    global $data_finish;
    global $data_start;		
    
	$valori = array();	        
    $cine = array();	

    function countComenzi($arrayValoriComenzi) {
    	$total = 0;
    	foreach ($arrayValoriComenzi as $key => $value)
    		if (!empty($value)) $total++;
    	return $total;	
    }
    
    function umple_valori($datas, $dataf, $cine, $valori) {
        global $valori;
        global $cine;
        global $data_finish;
        global $data_start;
                    	
		$data_start = $datas;
		$data_finish = $dataf;
		$arr1 = explode("-", $data_start);
		$arr2 = explode("-", $data_finish);
		
		$period = gregoriantojd($arr2[1], $arr2[2], $arr2[0]) - gregoriantojd($arr1[1], $arr1[2], $arr1[0]) + 1;
		$period .= " DAY ";
		
		$st = gregoriantojd($arr1[1], $arr1[2], $arr1[0]);
		$fn = gregoriantojd($arr2[1], $arr2[2], $arr2[0]);

		for ($i = $st; $i <= $fn; $i++) {
			$temp_date_arr = cal_from_jd($i, CAL_GREGORIAN);
			
			if ($temp_date_arr['month'] <= 9) {
				$luna = "0" . $temp_date_arr['month'];
			}
			else $luna = $temp_date_arr['month'];
			
			if ($temp_date_arr['day'] <= 9) {
				$zi = "0" . $temp_date_arr['day'];
			}
			else $zi = $temp_date_arr['day'];
			
			$temp_date = $temp_date_arr['year'] . "-" . $luna . "-" . $zi;
			$valori[$temp_date] = 0;
			$cine[] = $temp_date;
		}	
    }

	if ($perioada == 'luna_curenta') {		
        
        global $valori;
        global $cine;   
        global $data_finish;
        global $data_start;                
         	
		$period = " 1 MONTH";		
		
		$data_finish = date("Y-m-d");
		$data_temp = cal_from_jd(gregoriantojd(date("m"),date("d"), date("Y")) - cal_days_in_month(CAL_GREGORIAN, date("m"), date("Y")), CAL_GREGORIAN);
		
		if ($data_temp['month'] <= 9) {
			$luna = "0" . $data_temp['month'];
		}
		else $luna = $data_temp['month'];

		if ($data_temp['day'] <= 9) {
			$zi = "0" . $data_temp['day'];
		}
		else $zi = $data_temp['day'];
				
		$data_start = $data_temp['year'] . "-" . $luna . "-" . $zi;				
		$data_finish = date("Y-m-d");	        
	}		
    
	if ($perioada == '3_luni') {			
		
		$period = " 3 MONTH";
		global $data_finish;
        global $data_start;                
		$data_finish = date("Y-m-d");
		
		$luna_curenta = date("n");
		$an_curent = date("Y");
		
		$zile_dif = cal_days_in_month(CAL_GREGORIAN, $luna_curenta, $an_curent);
		
		for ($i = 1; $i <=3; $i++) {
			if ($luna_curenta == "1") {
				$luna_curenta = "12";
				$an_curent--;				
			}
			else $luna_curenta = $luna_curenta - 1;
			$zile_dif += cal_days_in_month(CAL_GREGORIAN, $luna_curenta, $an_curent);			
		}
		
		$data_temp = cal_from_jd(gregoriantojd(date("m"),date("d"), date("Y")) - $zile_dif, CAL_GREGORIAN);
		
		if ($data_temp['month'] <= 9) {
			$luna = "0" . $data_temp['month'];
		}
		else $luna = $data_temp['month'];

		if ($data_temp['day'] <= 9) {
			$zi = "0" . $data_temp['day'];
		}
		else $zi = $data_temp['day'];		
		
		$data_start = $data_temp['year'] . "-" . $luna . "-" . $zi;			
		$data_finish = date("Y-m-d");			
	}		
	
	if ($perioada == '6_luni') {			
		$period = " 6 MONTH";
        global $data_finish;
        global $data_start;                
        
		$data_finish = date("Y-m-d");
		
		$luna_curenta = date("n");
		$an_curent = date("Y");
		
		$zile_dif = cal_days_in_month(CAL_GREGORIAN, $luna_curenta, $an_curent);
		
		for ($i = 1; $i <= 5; $i++) {	
			if ($luna_curenta == "1") {
				$luna_curenta = "12";
				$an_curent--;	
				
			}
			else $luna_curenta = $luna_curenta - 1;
			
			$zile_dif += cal_days_in_month(CAL_GREGORIAN, $luna_curenta, $an_curent);			
		}

		$data_temp = cal_from_jd(gregoriantojd(date("m"),date("d"), date("Y")) - $zile_dif, CAL_GREGORIAN);
		
		if ($data_temp['month'] <= 9) {
			$luna = "0" . $data_temp['month'];
		}
		else $luna = $data_temp['month'];

		if ($data_temp['day'] <= 9) {
			$zi = "0" . $data_temp['day'];
		}
		else $zi = $data_temp['day'];		
		
		$data_start = $data_temp['year'] . "-" . $luna . "-" . $zi;	
		$data_finish = date("Y-m-d");					
	}		
	
	if ($perioada == '1_an') {			

		$period = " 12 MONTH";		
        global $data_finish;
        global $data_start;                

		$data_finish = date("Y-m-d");
		
		$luna_curenta = date("n");
		$an_curent = date("Y");
		
		$zile_dif = cal_days_in_month(CAL_GREGORIAN, $luna_curenta, $an_curent);
		
		for ($i = 1; $i <= 11; $i++) {	
			if ($luna_curenta == "1") {
				$luna_curenta = "12";
				$an_curent--;			
			}
			else $luna_curenta = $luna_curenta - 1;
			$zile_dif += cal_days_in_month(CAL_GREGORIAN, $luna_curenta, $an_curent);			
		}
		
		$data_temp = cal_from_jd(gregoriantojd(date("m"),date("d"), date("Y")) - $zile_dif, CAL_GREGORIAN);
		
		if ($data_temp['month'] <= 9) {
			$luna = "0" . $data_temp['month'];
		}
		else $luna = $data_temp['month'];

		if ($data_temp['day'] <= 9) {
			$zi = "0" . $data_temp['day'];
		}
		else $zi = $data_temp['day'];		
		
		$data_start = $data_temp['year'] . "-" . $luna . "-" . $zi;	
		$data_finish = date("Y-m-d");					
	}
    
	if (strlen($perioada) > 12 ) {
		$date_arr = explode('and', $perioada);
		
        global $data_finish;
        global $data_start;                
        
		$data_start = $date_arr[0];
		$data_finish = $date_arr[1];
						
		$arr1 = explode("-", $data_start);
		$arr2 = explode("-", $data_finish);
        
		$period = gregoriantojd($arr2[1], $arr2[0], $arr2[2]) - gregoriantojd($arr1[1], $arr1[0], $arr1[2]) + 1;		
		$period .= " DAY ";
        
        $d1 = $date_arr[0];
        $d1_arr = explode("-", $d1);
        $data_start = $d1_arr[2] . "-" . $d1_arr[1] . "-" . $d1_arr[0];          
        
        $d2 = $date_arr[1];
        $d2_arr = explode("-", $d2);
        $data_finish = $d2_arr[2] . "-" . $d2_arr[1] . "-" . $d2_arr[0];          
	}    
	
	if ($perioada == 'tot') {
		
        global $data_finish;
        global $data_start;    
               
        $query = "SELECT MAX(data_comenzii) AS data_finish, MIN(data_comenzii) AS data_start FROM comanda";     
        $result = $db->query($query); 
        
        $data = $result->fetch(PDO::FETCH_ASSOC);

		$data_start = $data['data_start'];
		$data_finish = $data['data_finish'];	
	}

    if ($criteriu == 'profit' || $criteriu == 'comenzi' || $criteriu == 'plati' || $criteriu == 'incasari') umple_valori($data_start, $data_finish, $cine, $valori);      
            
	if ($criteriu == 'profit' || $criteriu == 'plati' || $criteriu == 'incasari') {
        $value = " SUM(suma_totala_sol) AS ss, SUM(suma_totala_exec) AS se, moneda_sol, moneda_exec ";  
        $profit_group_by1 = ", moneda_sol ";  
        $profit_group_by2 = ", moneda_exec ";  
    }
    else {
    	$profit_group_by1 = "";
    	$profit_group_by2 = "";
    }
    if ($criteriu == 'comenzi') {
        $value = " COUNT(*) AS val  ";    
    }
    		
	if ($user > -1) {
		 $condition_user = " AND created_by = $user ";
	}
	else $condition_user = "";
    
    if ($user == -2) {
         $user_group_by = " , comanda.created_by ";
         $user_select = " , user.nume AS n, user.prenume AS p ";
         $user_left_join = " LEFT JOIN user ON comanda.created_by = user.id ";
    }
    else {
        $user_group_by = "";        
        $user_left_join = "";
        $user_select = "";
    }

	if ($client > -1) {
		 $condition_client = " AND id_firma_sol = $client ";
	}
	else $condition_client = "";
	
	if ($trans > -1) {
		 $condition_transportator = " AND id_firma_exec = $trans ";
	}
	else $condition_transportator = "";
    
    if ($incarcare > -1 || $descarcare > -1) {
         $incarcare_descarcare_left_join = " LEFT JOIN adresa ON comanda.id = adresa.id_owner "; 
         $incarcare_descarcare_condition1 = " AND adresa.tip = " . CT_ADRESA_COMANDA . " "; 
         $incarcare_descarcare_group_by = " , adresa.id_owner ";
    }
    else {
         $incarcare_descarcare_left_join = "";                
         $incarcare_descarcare_condition1 = "";
         $incarcare_descarcare_group_by = "";       
    }
    
    if ($incarcare > -1) {             
         $incarcare_condition2 = " AND adresa.tip_loc = " . CT_ADRESA_COMANDA_INCARCARE . " ";
    }
    else {
         $incarcare_condition2 = "";             
    }        
    
    if ($descarcare > -1) {
         $descarcare_condition2 = " AND adresa.tip_loc = " . CT_ADRESA_COMANDA_DESCARCARE . " ";
    }
    else {
         $descarcare_condition2 = "";             
    }            

    if ($criteriu == 'comenzi' || $criteriu == 'profit' || $criteriu == 'plati' || $criteriu == 'incasari') {   
    	         
	    $query = "SELECT $value, data_comenzii, created_by $user_select										
    				    FROM comanda
                        $incarcare_descarcare_left_join
                        $user_left_join
					    WHERE comanda.id_client = $id_client
						    AND comanda.deleted = " . CT_COMANDA_NESTERS;
						    
		if ($perioada != "tot")						    
			$query .= "            							
						    AND data_comenzii >= DATE_SUB('" . $data_finish . "', INTERVAL $period)
						    AND data_comenzii <= '{$data_finish}'";
		$query .= "
						    $condition_user                            
						    $condition_client
                            $condition_transportator
                            $incarcare_descarcare_condition1
                            $incarcare_condition2
                            $descarcare_condition2
					    GROUP BY data_comenzii $incarcare_descarcare_group_by $user_group_by $profit_group_by1 $profit_group_by2";		
//		echo $query;
        $result = $db->query($query);        

        if ($criteriu == 'comenzi') {
            $arr_user_id = array();
            if ($user == -2) {
                while ($data = $result->fetch(PDO::FETCH_ASSOC)) {             
                    $user_id = $data['n'] . " " . $data['p'];
                    if (!in_array($user_id, $arr_user_id)) $arr_user_id[] = $user_id;                
                    $valori[$user_id][$data['data_comenzii']] = (int)($data['val']);                  
                }
            }
            else {
                array_fill(0, count($cine), 0);
                while ($data = $result->fetch(PDO::FETCH_ASSOC)) {     
                    $valori[$data['data_comenzii']] = (int)($data['val']);    
                }        
            }            
        }
           
        if ($criteriu == 'profit') {
            $arr_user_id = array();
            if ($user == -2) {
                while ($data = $result->fetch(PDO::FETCH_ASSOC)) {             
                    $user_id = $data['n'] . " " . $data['p'];
                    if (!in_array($user_id, $arr_user_id)) $arr_user_id[] = $user_id;   
                    
                    $valori[$user_id][$data['data_comenzii']] += ($data['ss'] * (1 / $$monede_simpu[$data['moneda_sol']])) - ($data['se'] * (1 / $$monede_simpu[$data['moneda_exec']]));
                }
            }
            else {
                array_fill(0, count($cine), 0);
                while ($data = $result->fetch(PDO::FETCH_ASSOC)) {     
                    if ($$monede_simpu[$data['moneda_exec']] == 0) $$monede_simpu[$data['moneda_exec']] = 1;              
                    if ($$monede_simpu[$data['moneda_sol']] == 0) $$monede_simpu[$data['moneda_sol']] = 1;              
                    $valori[$data['data_comenzii']] += ($data['ss'] * (1 / $$monede_simpu[$data['moneda_sol']])) - ($data['se'] * (1 / $$monede_simpu[$data['moneda_exec']]));
                    $a = $monede_simpu[$data['moneda_exec']];
                }        
            }             
        }
        
        if ($criteriu == 'incasari') {
            $arr_user_id = array();
            if ($user == -2) {
                while ($data = $result->fetch(PDO::FETCH_ASSOC)) {             
                    $user_id = $data['n'] . " " . $data['p'];
                    if (!in_array($user_id, $arr_user_id)) $arr_user_id[] = $user_id;   
                    
                    $valori[$user_id][$data['data_comenzii']] += ($data['ss'] * (1 / $$monede_simpu[$data['moneda_sol']]));
                }
            }
            else {
//            	var_dump($cine);
                array_fill(0, count($cine), 0);
                while ($data = $result->fetch(PDO::FETCH_ASSOC)) {     
                    if ($$monede_simpu[$data['moneda_exec']] == 0) $$monede_simpu[$data['moneda_exec']] = 1;              
                    if ($$monede_simpu[$data['moneda_sol']] == 0) $$monede_simpu[$data['moneda_sol']] = 1;              
                    $valori[$data['data_comenzii']] += ($data['ss'] * (1 / $$monede_simpu[$data['moneda_sol']]));
                    $a = $monede_simpu[$data['moneda_exec']];
                }        
            }  
//            var_dump($valori);           
        }   
        
        if ($criteriu == 'plati') {
            $arr_user_id = array();
            if ($user == -2) {
                while ($data = $result->fetch(PDO::FETCH_ASSOC)) {             
                    $user_id = $data['n'] . " " . $data['p'];
                    if (!in_array($user_id, $arr_user_id)) $arr_user_id[] = $user_id;   
                    
                    $valori[$user_id][$data['data_comenzii']] += ($data['se'] * (1 / $$monede_simpu[$data['moneda_exec']]));
                }
            }
            else {
                array_fill(0, count($cine), 0);
                while ($data = $result->fetch(PDO::FETCH_ASSOC)) {     
                    if ($$monede_simpu[$data['moneda_exec']] == 0) $$monede_simpu[$data['moneda_exec']] = 1;              
                    if ($$monede_simpu[$data['moneda_sol']] == 0) $$monede_simpu[$data['moneda_sol']] = 1;              
                    $valori[$data['data_comenzii']] += ($data['se'] * (1 / $$monede_simpu[$data['moneda_exec']]));
                    $a = $monede_simpu[$data['moneda_exec']];
                }        
            }             
        }               
    }        
         
    
    if (count($valori) < 5) { ?>
   
	<legend_label layout='horizontal' 
		         bullet='circle'
		         font='arial'
		         bold='true'
		         size='12'
		         color='FFFFFF'
		         alpha='90' />	             	
   <?php 
	}
   	if ((count($cine) > 0) && (count($cine) <= 15)) $per = 0;   		
   	else if ((count($cine) >= 15) && (count($cine) <= 31)) $per = 2;   		
   	else if ((count($cine) > 31) && (count($cine) < 60)) $per = 3;   		
   	else if ((count($cine) >= 60) && (count($cine) < 90)) $per = 20;   		   			
   	else if ((count($cine) >= 90) && (count($cine) < 130)) $per = 15;   		
   	else if ((count($cine) >= 130)) $per = 25;   		
   	echo "<axis_category skip='{$per}' font='arial' bold='true' size='10' color='000000' alpha='100' orientation='diagonal_down' />";   			           		

	if (count($valori) < 5) { ?>   
   <legend_rect x='5'
                y='5'
                width='590' 
                height='10' 
                margin='5'
                fill_color='FFFFFF'
                line_color='FFFFFF'
                line_thickness='1'
                /> 	
   <?php
		}
	?>

 <chart_type><?php echo $tip; ?></chart_type>       
	<chart_data>
		<?php
			
            echo "<row>\n";                    
                echo "<null/>\n";
                for ($k = 0; $k < count($cine); $k++) {                
                    echo "<string>" . getDateForUser($cine[$k]) . "</string>\n";                        
                }                                                        
            echo "</row>\n";                

            if ($user == -2) {
                
                for ($i = 0; $i < count($arr_user_id); $i++) {
                    echo "<row>\n";
                        echo "<string>{$arr_user_id[$i]}</string>\n";
                        
                        for ($k = 0; $k < count($cine); $k++) {   
                            if (empty($valori[$arr_user_id[$i]][$cine[$k]])) {
                                echo "<number>0</number>\n";
                            }                             
                            else echo "<number>" . ($valori[$arr_user_id[$i]][$cine[$k]]). "</number>\n";
                        }
                    echo "</row>\n";                                
                }            
            }
            else {
            	if (empty($valori)) $valori[] = 0;
                echo "<row>\n";            
                	if ($criteriu == 'profit')
                    	echo "<string>$criteriu in {$_GET['moneda']} - TOTAL = " . array_sum($valori) . "</string>\n";
                    else 
                    	echo "<string>$criteriu - TOTAL = " . countComenzi($valori) . "</string>\n";
                    for ($k = 0; $k < count($cine); $k++) {                                
                        echo "<number>" . ($valori[$cine[$k]]). "</number>\n";
                    }            
                echo "</row>\n";                            
            }        
		?>
	</chart_data>
</chart>
